#include "DELAY.h"

void Delay_Init(void){
	// 开启时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
	
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period = 0xffff;
	TIM_TimeBaseInitStruct.TIM_Prescaler = 71;
	
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
	//开启外设
  TIM_Cmd(TIM2, ENABLE);
//	
//	TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
//	
//	NVIC_InitTypeDef NVIC_InitStruct;
//	NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
//	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
//	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
//	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
//	NVIC_Init(&NVIC_InitStruct);
}

void Delay_us(uint16_t us){
		TIM_SetCounter(TIM2, 0);
		while(TIM_GetCounter(TIM2) < us);
}

void Delay_ms(uint16_t ms){
	for(int i =0;i<ms;i++){
		Delay_us(1000);
	}
}

